package com.jiamao.goplay.ui.login;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import me.goldze.mvvmhabit.utils.RxUtils;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.LoginRegisterUserinfoAddBasicInfoLayoutBinding;
import com.jiamao.goplay.ui.login.vm.RegisterAddBasicInfoModel;
import com.jiamao.goplay.util.KeyInputUtils;
import com.jiamao.goplay.util.TimeUtils;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.v3.BottomMenu;
import com.tamsiree.rxkit.view.RxToast;

import java.util.Calendar;
import java.util.Date;

/**
 * 添加基础资料Fragment
 * create by 2020-11-4
 */
public class AddBasicInfoFragment extends BaseMVVMFragment<LoginRegisterUserinfoAddBasicInfoLayoutBinding, RegisterAddBasicInfoModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.login_register_userinfo_add_basic_info_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }




    @Override
    public RegisterAddBasicInfoModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(RegisterAddBasicInfoModel.class);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.showSex.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                showSexMenu();
            }
        });

        viewModel.uc.showTime.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                showTimeMenu();
            }
        });

    }

    @Override
    public void initData() {
        super.initData();


        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();

        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("");
        binding.getRoot().findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });


        binding.editNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                aVoidData();
            }
        });

        binding.editNickName.setSelection(  binding.editNickName.getText().length());
        new Handler().postDelayed(new Runnable(){
            public void run() {
                binding.editNickName.requestFocus();
                KeyInputUtils.showInputMethodWindow(binding.editNickName);
            }
        }, 500);

    }


    private void showSexMenu(){
        KeyInputUtils.hideSoftInputView(getActivity());
        BottomMenu.show((AppCompatActivity) getContext(), new String[]{"男", "女"}, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
             //   RxToast.showToastShort(text);
                switch (index){
                    case 0:
                        viewModel.sex.set("男");
                        //startContainerActivity(SearchFragment.class.getCanonicalName());
                        break;
                    case 1:
                        viewModel.sex.set("女");
                        break;
                    case 2:
                        //startContainerActivity(CityFragment.class.getCanonicalName());
                        break;
                }
                aVoidData();
            }
        });
    }


    private void showTimeMenu(){
        KeyInputUtils.hideSoftInputView(getActivity());
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        selectedDate.set(1992,5,15);
        Calendar startDate = Calendar.getInstance();
        startDate.set(1900, 0, 31);

        Calendar endDate = Calendar.getInstance();
       // endDate.set(, 12, 31);





        TimePickerView pvTime = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                //Toast.makeText(getContext(), date., Toast.LENGTH_SHORT).show();
                viewModel.constellation.set(TimeUtils.getTime(date)+" "+TimeUtils.getAstro(date));
                viewModel.constellations.set(TimeUtils.getAstro(date));
                viewModel.birthday.set(TimeUtils.getTime(date));
                aVoidData();
            }
        }).       setDate(selectedDate)
                .setRangDate(startDate,endDate)
                .build();
        pvTime.show();
    }


    public void aVoidData(){
       if( !TextUtils.isEmpty(viewModel.nickName.get()) && !TextUtils.isEmpty(viewModel.constellation.get()) && !TextUtils.isEmpty(viewModel.sex.get()) ){
           binding.tvNext.setEnabled(true);
           binding.tvNext.setBackgroundResource(R.drawable.bg_login_btn_ok);
           binding.tvNext.setTextColor(Color.WHITE);
       }else {
           binding.tvNext.setEnabled(false);
           binding.tvNext.setBackgroundResource(R.drawable.bg_login_btn_default);
           binding.tvNext.setTextColor(Color.parseColor("#d0d0d0"));
       }


    }


}
